﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Command;
using Structure.Interfaces;

namespace Application.Controls
{
    public partial class TaskList : System.Web.UI.UserControl
    {
        private iCommand _command = BuildCommand.Build();
        public event Action<string> onEdit;

        protected void Page_Load(object sender, EventArgs e)
        {
            try 
            {
                if (!IsPostBack)
                {
                    // добавляем кнопки удаления          
                    DataControlField DeleteButtons = new ButtonField();
                    (DeleteButtons as ButtonField).CommandName = "DeleteRow";
                    (DeleteButtons as ButtonField).ButtonType = ButtonType.Image;
                    (DeleteButtons as ButtonField).ImageUrl = "~/images/delete.png";
                    TasksGridView.Columns.Add(DeleteButtons);

                    // добавляем кнопки редактирования
                    DataControlField EditButtons = new ButtonField();
                    (EditButtons as ButtonField).CommandName = "EditRow";
                    (EditButtons as ButtonField).ButtonType = ButtonType.Image;
                    (EditButtons as ButtonField).ImageUrl = "~/images/edit.gif";
                    TasksGridView.Columns.Add(EditButtons);
                }

               // параметры таблицы
                TasksGridView.AllowSorting = true;
            
                // получаем список проектов
                // iCommand _command;
                // _command = BuildCommand.Build();
            
                TasksGridView.DataSource = _command.TaskList();
                TasksGridView.DataBind();

            }
            catch (Exception ex)
            {}
        }

        protected void TasksGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeleteRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = TasksGridView.DataSource as DataTable;

                    string id_task = dt.Rows[index]["#"].ToString();

                    _command.DeleteTask(id_task);

                    dt.Rows.RemoveAt(index);

                    TasksGridView.DataSource = dt;
                    TasksGridView.DataBind();
                }

                else if (e.CommandName == "EditRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = TasksGridView.DataSource as DataTable;

                    string id_task= dt.Rows[index]["#"].ToString();

                    onEdit(id_task);

                    //Response.AddHeader("id_project", id_project);
                    //Response.AddHeader("action", "edit");
                    //Response.Redirect("~/Settings/ProjectForm.aspx");
                }
            }
            catch (Exception ex)
            {}
        }
        protected void TasksGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {}
            catch (Exception ex)
            {}
        }
        public void Update()
        {
            TasksGridView.DataSource = _command.TaskList();
            TasksGridView.DataBind();
        }        

        protected void btSelectTask_Click1(object sender, EventArgs e)
        {
            int id_task = Convert.ToInt32(SelectTask.Text);
            TasksGridView.DataSource = _command.SelectTask(id_task);
            TasksGridView.DataBind();
        }
    }
}